package com.iespring.controller.app.order;

import com.iespring.bean.admin.order.pojo.OrderGoods;
import com.iespring.bean.app.order.bo.CommentBO;
import com.iespring.bean.app.order.bo.OrderChangeStatusBO;
import com.iespring.bean.app.order.bo.OrderSubmitBO;
import com.iespring.bean.app.order.vo.OrderForDetailVO;
import com.iespring.bean.app.order.vo.OrderSubmitVO;
import com.iespring.bean.common.BaseParam;
import com.iespring.bean.common.BaseRespVo;
import com.iespring.bean.common.CommonData;
import com.iespring.service.app.order.WxOrderService;
import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.PortUnreachableException;

/**
 * @Author: 浩澄
 * @Date: 2022/12/31-16:08
 * @Mission: # 小程序订单模块
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;

    @RequestMapping("list")
    public BaseRespVo list(BaseParam baseParam, Integer showType) {

        CommonData data = wxOrderService.list(baseParam, showType);

        return BaseRespVo.ok(data);
    }

    //TODO 完成提交页
    //提交订单submit
    @RequestMapping("submit")
    public BaseRespVo submit(@RequestBody OrderSubmitBO orderSubmitBO){

        OrderSubmitVO orderSubmitVO = wxOrderService.submit(orderSubmitBO);

        return BaseRespVo.ok(orderSubmitVO);
    }

    //支付
    @RequestMapping("prepay")
    public BaseRespVo prepay(@RequestBody OrderChangeStatusBO orderChangeStatusBO){

        int orderStatus = 201;

        int code = wxOrderService.orderChangeStatus(orderChangeStatusBO.getOrderId(), orderStatus);

        if (code == 200){

            return BaseRespVo.ok();
        }else {
            return BaseRespVo.error("订单不能支付",724);
        }
    }


    //订单详情
    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId) {

        OrderForDetailVO orderForDetailVO = wxOrderService.detail(orderId);

        return BaseRespVo.ok(orderForDetailVO);
    }

    //取消订单
    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody OrderChangeStatusBO orderChangeStatusBO) {

        int orderStatus = 102;

        int code = wxOrderService.orderChangeStatus(orderChangeStatusBO.getOrderId(), orderStatus);

        if (code == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.error("订单取消失败", 724);
        }
    }

    //确认收货
    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody OrderChangeStatusBO orderChangeStatusBO) {

        int orderStatus = 401;

        int code = wxOrderService.orderChangeConfirmStatus(orderChangeStatusBO.getOrderId(), orderStatus);

        if (code == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.error("订单确认收货失败", 724);
        }
    }

    //申请退款
    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody OrderChangeStatusBO orderChangeStatusBO){

        int orderStatus = 202;

        int code = wxOrderService.orderRefund(orderChangeStatusBO.getOrderId(),orderStatus);

        if (code == 200){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.error("订单申请退款失败",724);
        }
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody OrderChangeStatusBO orderChangeStatusBO){

        int code = wxOrderService.delete(orderChangeStatusBO.getOrderId());

        if (code == 200){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.error("删除订单失败",724);
        }
    }

    //收货未评价的去评价
    @RequestMapping("goods")
    public BaseRespVo goods(Integer orderId,Integer goodsId){

        OrderGoods orderGoods = wxOrderService.goods(orderId,goodsId);

        return BaseRespVo.ok(orderGoods);
    }

    //订单提交评价
    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody CommentBO commentBO){

        int code = wxOrderService.comment(commentBO);

        if (code == 200){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.error("商品评价失败",724);
        }
    }


}
